import agree from '@/components/text-agreement/settlement.vue'//协议
import taddress from '@/components/threeAddress.vue'//选择地区
export default {
  components: {
    agree,
    taddress
  },
  data() {
    return {
      isRz: false,//'是否认证'
      name: '公司入驻',
      list: [{
        name: '公司入驻',
        disabled: false
      },
      {
        name: '个人申请',
        disabled: false
      }
      ],
      value: '公司入驻', //type值
      upload: [{
        type: 1,
        icon: 'iconxiangji',
        text: '营业执照',
        src: '',
      },
      {
        type: 2,
        icon: 'iconxiangji',
        text: '法人身份证正面',
        src: '',
      },
      {
        type: 3,
        icon: 'iconxiangji',
        text: '法人身份证反面',
        src: '',
      }
      ],
      isAgree: true, //是否勾选
      model: false, //模态框
      type: false, // 公司类型
      address: false, //区域弹出
      //公司类型列表
      typeList: [{
        name: '有限责任公司',
        type: 0,
      },
      {
        name: '股份有限公司',
        type: 1,
      },
      {
        name: '有限合伙企业',
        type: 2,
      },
      {
        name: '外商公司',
        type: 3,
      },
      {
        name: '个人独资',
        type: 4,
      },
      {
        name: '政府企事业单位',
        type: 5,
      }
      ],
      typeValue: '选择公司类型',
      typeId: 0, //公司类型
      addressList: [], //地址信息
      addressId: [],
      //公司入驻
      settlement: {
        name: '', //联系人姓名
        address: '', //公司地址
        fullName: '', //公司全称
        phone: '', //联系电话
        license: '', //营业执照
        faceimg: '', //身份证正面
        backimg: '', //身份证反面
      },

      //个人申请数据
      personal: {
        name: '', //联系人姓名
        address: '', //公司地址
        phone: '', //联系电话
        remarks: '', //备注
      }
    }
  },
  
  computed: {
    serviceNumber(){
      return this.$store.state.serviceNumber
    }
  },

  methods: {
    close() {
		  // this.address = false
    },
    phone(){
      uni.makePhoneCall({
			  phoneNumber: this.serviceNumber.number
      })
    },
    //返回方法
    back(){
      uni.navigateBack({
				  delta: 1
      })
    },
    //协议
    agreement() {
      this.isAgree = true
    },
    // 公司类型服务商入驻
    async insertCompnayService() {
      uni.showLoading({
			  title: '加载中',
        mask: true
      })
      try {
        let data = {
          'province': this.addressId.province,
          'city': this.addressId.city,
          'district': this.addressId.area,
          'mobile': this.settlement.phone,
          'business_License_Path': this.upload[0].src, //营业执照存放地址
          'faceimg': this.upload[1].src, //身份证正面图片
          'backimg': this.upload[2].src, //身份证背面图片
          'company_Type': this.typeId //公司类型
        }
        const res = await this.$api.me.insertCompnayService(data)
        uni.hideLoading()
        if(res.code===200){
          this.$u.toast('申请成功')
          this.$u.debounce(this.back, 2000)
        }
      } catch (e) {

      }
    },

    //个人类型服务商入驻
    async insertPersonService() {
      uni.showLoading({
			  title: '加载中'
      })
      try {
        let data = {
          'remark': this.personal.remarks, //备注
          'linkman': this.personal.name, //联系人名字
          'mobile': this.personal.phone, //电话
          'province': Number(this.addressId.province), //省市区
          'city': Number(this.addressId.city),
          'district': Number(this.addressId.area),
          'address': this.personal.address
        }
        const res = await this.$api.me.insertPersonService(data)
        console.log(res, '个人')
        uni.hideLoading()
        if(res.code===200){
          this.$u.toast('申请成功')
          this.$u.debounce(this.back, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },

    //验证营业执照
    async checkBusinessLicense() {
      /* uni.showLoading({
        title: '验证中'
      }) */
      try {
        let data = {
          'imgURL': this.upload[0].src //营业执照图片路径
        }
        const res = await this.$api.me.checkBusinessLicense(data)
        // uni.hideLoading()
      } catch (e) {
        //TODO handle the exception
      }
    },

    //身份证验证
    async checkIDCard() {
      if (this.upload[1].src === '' || this.upload[2].src === '') return false
      /* uni.showLoading({
        title: '验证中'
      }) */
      try {
        let data = {
          'frontImg': this.upload[1].src, //身份证正面
          'backImg': this.upload[2].src //身份证反面
        }
        const res = await this.$api.me.checkIDCard(data)
        // uni.hideLoading()
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 单选
    radioChange(name) {
      this.name = name
      console.log(name)
    },
    //申请区域
    applicationArea(item) {
      this.address = false
      console.log(item)
      this.addressList.area = item.area.label
      this.addressList.city = item.city.label
      this.addressList.province = item.province.label
      this.addressId.area = Number(item.area.value)
      this.addressId.city = Number(item.city.id)
      this.addressId.province = Number(item.province.id)
      // console.log(this.addressId)
    },

    //选择公司类型
    selectType(item) {
      this.type = false
      this.typeValue = item.name
      this.typeId = item.type
    },
    //上传证件
    onUpload(type) {
      console.log(type)
      if (type === 1) {
        this.uploadlicense()
      } else if (type === 2) {
        this.uploadfaceimg()
      } else if (type === 3) {
        this.uploadbackimg()
      }
    },
    //上传营业执照
    uploadlicense() {
      uni.chooseImage({
        count: 1,
        success: e => {
          uni.showLoading({
            title: '上传中'
          })
          this.$api.files.uploadImg({
            filePath: e.tempFilePaths[0]
          }).then(res => {
            uni.hideLoading()
            this.upload[0].src = res.baseUrl + res.url
            this.checkBusinessLicense()
          }).catch(err => {
            uni.hideLoading()
          })
        }
      })

    },

    //上传身份证正面
    uploadfaceimg() {
      uni.chooseImage({
        count: 1,
        success: e => {
          uni.showLoading({
            title: '上传中'
          })
          this.$api.files.uploadImg({
            filePath: e.tempFilePaths[0]
          }).then(res => {
            uni.hideLoading()
            console.log(res, 'img')
            this.upload[1].src = res.baseUrl + res.url
            this.checkIDCard()
          }).catch(err => {
            uni.hideLoading()
          })
        }
      })
    },
    //上传身份证反面
    uploadbackimg() {
      uni.chooseImage({
        count: 1,
        success: e => {
          uni.showLoading({
            title: '上传中'
          })
          this.$api.files.uploadImg({
            filePath: e.tempFilePaths[0]
          }).then(res => {
            uni.hideLoading()
            console.log(res, 'img')
            this.upload[2].src = res.baseUrl + res.url
            this.checkIDCard()
          }).catch(err => {
            uni.hideLoading()
          })
        }
      })
    },
    //同意
    agree() {
      this.isAgree = !(this.isAgree)
    },

    //提交
    submit(e) {
      console.log(e)
      if (this.isAgree === false) {
        this.model = true
      } else {
        if (this.name === '公司入驻') {
          if (this.settlement.phone === '') {
            this.$u.toast('电话号码不能为空')
            return false
          } else if (this.settlement.fullName === '') {
            this.$u.toast('公司全称不能为空')
            return false
          } else if (!this.addressList.city) {
            this.$u.toast('申请地址不能为空')
            return false
          } else if (this.settlement.address === '') {
            this.$u.toast('公司地址不能为空')
            return false
          } else if (!this.upload[0].src) {
            this.$u.toast('请上传营业执照')
            return false
          } else if (!this.upload[1].src) {
            this.$u.toast('请上传身份证正面图')
            return false
          } else if (!this.upload[2].src) {
            this.$u.toast('请上传身份证背面图')
            return false
          }
          this.insertCompnayService()
        } else if (this.name === '个人申请') {
          if (!this.addressList.city) {
            this.$u.toast('申请地址不能为空')
            return false
          } else if (this.personal.phone === '') {
            this.$u.toast('电话号码不能为空')
            return false
          } else if (this.personal.name === '') {
            this.$u.toast('姓名不能为空')
            return false
          } else if (this.personal.address === '') {
            this.$u.toast('公司地址不能为空')
            return false
          }
          this.insertPersonService()
        }
      }
    }
  }
}
